package com.wing.sell.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.sell.dao.AgentDueSubjectDao;
import com.wing.sell.model.entity.AgentDueSubject;
import com.wing.sell.model.entity.AgentDueSubjectCategory;
import com.wing.sell.model.entity.AgentDueSubjectCompany;
import com.wing.sell.model.request.AgentDueSubjectForm;
import com.wing.sell.model.response.AgentDueSubjectVO;
import com.wing.sell.service.AgentDueSubjectCompanyService;
import com.wing.sell.service.AgentDueSubjectService;
import com.wing.web.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * -ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class AgentDueSubjectServiceImpl extends ServiceImpl<AgentDueSubjectDao, AgentDueSubject> implements AgentDueSubjectService {

    @Autowired
    private AgentDueSubjectCompanyService agentDueSubjectCompanyService;

    @Override
    @Transactional(readOnly = false)
    public int add(AgentDueSubjectForm agentDueSubjectForm) {

        //校验
        AgentDueSubject agentDueSubject =new AgentDueSubject();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(AgentDueSubject.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(agentDueSubjectForm, agentDueSubject, copyOptions);

        int i = baseMapper.insert(agentDueSubject);
        if(i > 0){
            for (AgentDueSubjectCompany agentDueSubjectCompany : agentDueSubjectForm.getAgentDueSubjectCompanyList()) {
                agentDueSubjectCompany.setCreator(JwtUtils.getLoginUserId());
                agentDueSubjectCompany.setCreatorName(JwtUtils.getLoginUsername());
                agentDueSubjectCompany.setModifier(JwtUtils.getLoginUserId());
                agentDueSubjectCompany.setModifierName(JwtUtils.getLoginUsername());
                agentDueSubjectCompany.setSubjectId(agentDueSubject.getId());
            }
            agentDueSubjectCompanyService.saveBatch(agentDueSubjectForm.getAgentDueSubjectCompanyList());
        }
        return i;
    }

    @Override
    @Transactional(readOnly = false)
    public boolean update(AgentDueSubject agentDueSubject) {
        int i = baseMapper.updateById(agentDueSubject);
        if(i > 0){
            List<AgentDueSubjectCompany> addCompanys = new ArrayList<>();
            List<AgentDueSubjectCompany> updCompanys = new ArrayList<>();
            List<Long> delCompanys = new ArrayList<>();
            List<Long> ids = new ArrayList<>();
            for (AgentDueSubjectCompany agentDueSubjectCompany : agentDueSubject.getAgentDueSubjectCompanyList()) {
                if(agentDueSubjectCompany.getId() == null){
                    agentDueSubjectCompany.setCreator(JwtUtils.getLoginUserId());
                    agentDueSubjectCompany.setCreatorName(JwtUtils.getLoginUsername());
                    agentDueSubjectCompany.setModifier(JwtUtils.getLoginUserId());
                    agentDueSubjectCompany.setModifierName(JwtUtils.getLoginUsername());
                    agentDueSubjectCompany.setSubjectId(agentDueSubject.getId());
                    addCompanys.add(agentDueSubjectCompany);
                }else{
                    ids.add(agentDueSubjectCompany.getId());
                    updCompanys.add(agentDueSubjectCompany);
                }
            }
            // 查询旧数据
            List<AgentDueSubjectCompany> oldCompanys = agentDueSubjectCompanyService.list(new QueryWrapper<AgentDueSubjectCompany>().eq("subject_id", agentDueSubject.getId()));
            for (AgentDueSubjectCompany oldCompany : oldCompanys) {
                if(!ids.contains(oldCompany.getId())){
                    delCompanys.add(oldCompany.getId());
                }
            }
            if(addCompanys.size() > 0){
                agentDueSubjectCompanyService.saveBatch(addCompanys);
            }
            if(updCompanys.size() > 0){
                agentDueSubjectCompanyService.updateBatchById(updCompanys);
            }
            if(delCompanys.size() > 0){
                agentDueSubjectCompanyService.removeByIds(delCompanys);
            }
        }
        return i > 0;
    }

    @Override
    public List<AgentDueSubjectVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<AgentDueSubjectVO> pageList4app(Page page, Map<String, Object> params) {
        List<AgentDueSubjectVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public AgentDueSubjectVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    @Transactional(readOnly = false)
    public boolean delete(Long agentDueSubjectId) {
        int i = baseMapper.deleteById(agentDueSubjectId);
        if(i > 0){
            agentDueSubjectCompanyService.remove(new QueryWrapper<AgentDueSubjectCompany>().eq("subject_id", agentDueSubjectId));
        }
        return i > 0;
    }

    @Override
    public int enable(Long id) {
        int i = 0;
        AgentDueSubject agentDueSubject = baseMapper.selectById(id);
        if(agentDueSubject != null){
            agentDueSubject.setIsDeleted(true);
            i = baseMapper.updateById(agentDueSubject);
        }
        return i;
    }

    @Override
    public int disable(Long id) {
        int i = 0;
        AgentDueSubject agentDueSubject = baseMapper.selectById(id);
        if(agentDueSubject != null){
            agentDueSubject.setIsDeleted(false);
            i = baseMapper.updateById(agentDueSubject);
        }
        return i;
    }

}
